package com.cq.hd.order.service.impl;

import com.alibaba.fastjson.JSON;
import com.cq.hd.common.enums.biz.OrderSettleBillTradeStateEnum;
import com.cq.hd.common.enums.biz.OrderSettleBillTradeTypeEnum;
import com.cq.hd.common.utils.BeanUtils;
import com.cq.hd.order.api.dto.OrderSettleBillDto;
import com.cq.hd.order.api.dto.OrderSettleBillUpdateStateDto;
import com.cq.hd.order.api.vo.MerchantOrderAmtStatVo;
import com.cq.hd.order.api.vo.OrderSettleBillVo;
import com.cq.hd.order.factory.orderSettle.OrderSettleBillFactory;
import com.cq.hd.order.po.TbOrderSettleBillPo;
import com.cq.hd.order.mapper.TbOrderSettleBillMapper;
import com.cq.hd.order.service.TbOrderSettleBillService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

/**
 * <p>
 * 订单结算支付流水记录表 服务实现类
 * </p>
 *
 * @author mYunYu
 * @since 2024-01-23
 */
@Slf4j
@Service
public class TbOrderSettleBillServiceImpl extends ServiceImpl<TbOrderSettleBillMapper, TbOrderSettleBillPo> implements TbOrderSettleBillService {

    @Override
    public List<OrderSettleBillVo> listByDto(OrderSettleBillDto orderSettleBillDto) {
        return baseMapper.listByDto(orderSettleBillDto);
    }

    @Override
    public Boolean transactAmt(OrderSettleBillVo orderSettleBillVo) {
        return OrderSettleBillFactory.getOrderSelectHandler(orderSettleBillVo.getTradeType()).transactAmt(orderSettleBillVo);
    }

    @Override
    public OrderSettleBillVo getOrderSettleBillById(Long id) {
        TbOrderSettleBillPo tbOrderSettleBillPo = baseMapper.selectById(id);
        if (tbOrderSettleBillPo != null && tbOrderSettleBillPo.getDelState() == 0) {
            OrderSettleBillVo orderSettleBillVo = new OrderSettleBillVo();
            BeanUtils.copyProperties(tbOrderSettleBillPo, orderSettleBillVo);
            return orderSettleBillVo;
        }
        return null;
    }

    /**
     * 根据 orderSettleId，对订单结算支付流水记录表进行转账
     * 目录仅用在商户补贴转账
     */
    @Override
    public Boolean transferByOrderSettleBill(OrderSettleBillVo orderSettleBillVo) {
        return OrderSettleBillFactory.getOrderSelectHandler(orderSettleBillVo.getTradeType()).transfer(orderSettleBillVo);
    }

    /**
     * 对订单结算单进行月度结算
     *
     * @param monthSettleBillId 月度结算单id
     * @param orderSettleIds 订单结算单id
     * @return 影响条数
     */
    @Override
    public int monthSettleOrderSettleBills(Long monthSettleBillId, List<Long> orderSettleIds) {
        return baseMapper.updateOrderBillsByMonthSettle(monthSettleBillId, orderSettleIds);
    }

    /**
     * 获取商户待入账本金 & 补贴（已结算，未计入月度账单的补贴金额）
     *
     * @param merchantId 商户id
     * @return 商户待入账补贴
     */
    @Override
    public MerchantOrderAmtStatVo getMerchantPendingAmt(Long merchantId) {
        BigDecimal pendingMerchantAmt = baseMapper.sumMerchantPendingAmt(merchantId, OrderSettleBillTradeTypeEnum.MERCHANT_AMT.getValue());
        BigDecimal pendingSubsidyAmt = baseMapper.sumMerchantPendingAmt(merchantId, OrderSettleBillTradeTypeEnum.SUBSIDY_AMT.getValue());
        return new MerchantOrderAmtStatVo(pendingMerchantAmt, pendingSubsidyAmt);
    }

    /**
     * 获取商家待支付补贴金额
     * @param businessId
     * @return
     */
    @Override
    public BigDecimal getBusinessWaitPaySubsidyAmt(Long businessId) {
        return baseMapper.sumBusinessWaitPaySubsidyAmt(businessId);
    }

    @Override
    public Integer updateTradeState(OrderSettleBillUpdateStateDto orderSettleBillUpdateStateDto) {
        return baseMapper.updateTradeState(orderSettleBillUpdateStateDto);
    }

    @Override
    public void scanRecoverAmtTransfer() {
        OrderSettleBillDto orderSettleBillDto = new OrderSettleBillDto();
        orderSettleBillDto.setTradeType(OrderSettleBillTradeTypeEnum.RECOVER_AMT.getValue());
        orderSettleBillDto.setTradeStates(Arrays.asList(OrderSettleBillTradeStateEnum.WAIT.getValue(), OrderSettleBillTradeStateEnum.FAIL.getValue()));
        List<OrderSettleBillVo> orderSettleBillVos = listByDto(orderSettleBillDto);

        if (CollectionUtils.isNotEmpty(orderSettleBillVos)) {
            for (OrderSettleBillVo orderSettleBillVo : orderSettleBillVos) {
                log.info("HF未核销金额转账, item={}", JSON.toJSONString(orderSettleBillVo));
                transactAmt(orderSettleBillVo);
            }
        }

    }
}
